package com.qiku.base;

import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Map;

public class BaseServlet extends HttpServlet {

    /**
     * @param req  接收前端数据
     * @param resp 向前端写入数据
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String methodName = null;
        boolean b = false;
        //获取请求的 Content-Type
        String contentType = req.getHeader("Content-Type");
//        System.out.println("Base contentType:"+contentType);
        //判断如果contentType 是JSON格式  则调用ParseJSON
        if ("application/json;charset=utf-8".equalsIgnoreCase(contentType)) {
            //是JSON格式
            String json = parseJSON(req);
            //将json  转换为map
            Map map = JSON.parseObject(json, Map.class);
            methodName = (String) map.get("methodName");
            //将map 存入request对象
            req.setAttribute("map", map);
        } else if (contentType != null && contentType.contains("multipart/form-data")) {
            b = true;
            Class c = this.getClass();
            try {
                Method updateResource = c.getMethod("UpdateResource", HttpServletRequest.class, HttpServletResponse.class);
                updateResource.invoke(this, req, resp);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            methodName = req.getParameter("methodName");
            System.out.println("请求方法名：" + methodName);
        }

        // 1 获取参数 methodName 的值
        if (b != true){
            if (methodName != null) {
                try {
                    // 2 根据参数的值去调用不同的方法即可
                    Class c = this.getClass();
                    // 根据 methodName  获取要执行的方法对象
                    Method method = c.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
                    // 执行方法
                    method.invoke(this, req, resp);
                } catch (Exception e) {
                    System.out.println("功能不支持...");
                    e.printStackTrace();
                }
            } else {
                System.out.println("未获取到methodName的值......");
            }
        }
    }


    /**
     * POST请求格式为 application/json;charset=utf-8
     * 在这个方法中我们使用流的方式,获取到POST请求的数据
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }


    /**
     * 定义解析JSON数据的方法
     *
     * @param req 接受前端请求
     * @return
     */
    public String parseJSON(HttpServletRequest req) {
        try {
            //获取缓冲字符流
            BufferedReader reader = req.getReader();
            //准备StringBuilder 接收数据
            StringBuilder sb = new StringBuilder();
            //循环读
            String line = null;
            //将读到的内容返回
            while ((line = reader.readLine()) != null) {
                //并将读到的内容 追加到  sb中
                sb.append(line);
            }
            return sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
